#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
//
// Created by 86131 on 2023/10/24.
//

#include <iostream>
using namespace std;
template <typename T>
class ArrayList1 {
private:
    T* data;
    int size;
    int capacity;

public:
    ArrayList1() : data(nullptr), size(0), capacity(0) {}

    ~ArrayList1() {
        delete[] data;
    }

    // 添加元素
    void add(const T& item) {
        if (size == capacity) {
            // 扩展容量
            int newCapacity = capacity == 0 ? 1 : capacity * 2;
            T* newData = new T[newCapacity];
            for (int i = 0; i < size; i++) {
                newData[i] = data[i];
            }
            delete[] data;
            data = newData;
            capacity = newCapacity;
        }
        data[size] = item;
        size++;
    }

    // 获取指定位置的元素
    T& get(int index) {
        if (index >= 0 && index < size) {
            return data[index];
        } else {
            std::cout << "索引越界。" << std::endl;
            // 返回一个默认值，具体处理方式可以根据需求进行修改
            return data[0];
        }
    }

    // 更新指定位置的元素
    void set(int index, const T& item) {
        if (index >= 0 && index < size) {
            data[index] = item;
        } else {
            std::cout << "索引越界。" << std::endl;
        }
    }

    // 删除指定位置的元素
    void remove(int index) {
        if (index >= 0 && index < size) {
            for (int i = index; i < size - 1; i++) {
                data[i] = data[i + 1];
            }
            size--;
        } else {
            std::cout << "索引越界。" << std::endl;
        }
    }

    // 获取线性表的大小
    int getSize() const {
        return size;
    }
    //改变线性表大小
    void setSize(int size1){
        this->size = size1;
    }
    bool empty(){
        return this->size==0;
    }
    int end(){
        return data[size-1];
    }
    int index(int value){
        for (int i = 0; i < size - 1; i++) {
            if(value ==data[i]){return i;}
        }
        cout<<"未找到";
        return 0;
    }
};
//
// Created by 86131 on 2023/11/14.
//
template<typename T>
class MinHeap {
private:
    ArrayList1<T> heap;

    int parent(int index) {
        return (index - 1) / 2;
    }

    int leftChild(int index) {
        return 2 * index + 1;
    }

    int rightChild(int index) {
        return 2 * index + 2;
    }

    void swap(int& a, int& b) {
        int temp = a;
        a = b;
        b = temp;
    }

    void siftUp(int index) {
        while (index > 0 && heap.get(index) < heap.get(parent(index))){
            swap(heap.get(index), heap.get(parent(index)));
            index = parent(index);
        }
    }

    void siftDown(int index) {
        int maxIndex = index;
        int leftIndex = leftChild(index);
        int rightIndex = rightChild(index);

        if (leftIndex < heap.getSize() && heap.get(leftIndex) < heap.get(maxIndex)) {
            maxIndex = leftIndex;
        }

        if (rightIndex < heap.getSize() && heap.get(rightIndex) < heap.get(maxIndex)) {
            maxIndex = rightIndex;
        }

        if (index != maxIndex) {
            swap(heap.get(index), heap.get(maxIndex));
            siftDown(maxIndex);
        }
    }

public:
    void insert(int value) {
        heap.add(value);
        siftUp(heap.getSize() - 1);
    }

    int extractMin() {
        if (heap.empty()) {
            return -1; // or any other appropriate handling for an empty heap
        }

        int max = heap.get(0);
        swap(heap.get(0), heap.get(heap.getSize() - 1));
        heap.remove(heap.getSize()-1);
        siftDown(0);
        return max;
    }
    int getMax(){
        return heap.get(0);
    }
    int getsize(){
        return heap.getSize();
    }

};
//
// Created by 86131 on 2023/11/14.
//


int main(void){
    MinHeap<int > q;
    int n;
    char s[66];
    int p[66];
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        getchar();
        scanf("%c%d",&s[i],&p[i]);
        q.insert(p[i]);
    }
    int ans=0;
    //ans即为哈弗曼树带权路径长度和，也就是所对应的哈夫曼编码的长度
    while(q.getsize()>1){
        int x=q.extractMin();

        int y=q.extractMin();
        q.insert(x+y);
        ans+=x+y;
    }
    int m;
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        char ss[66][66];
        int sum=0;
        for(int j=0;j<n;j++){
            getchar();
            char c;
            scanf("%c %s",&c,ss[j]);
            sum+=strlen(ss[j])*p[j];
        }
        if(sum!=ans)printf("No\n");
        else{
            int flag=0;
            for(int j=0;j<n;j++){
                int x=strlen(ss[j]);
                for(int l=0;l<n;l++){
                    if(j!=l){
                        if(strstr(ss[l],ss[j])==&ss[l][0]){
                            //查找字符串，如果找到了并且是前缀，就标记为No了
                            flag=1;
                            break;
                        }
                    }
                }
                if(flag==1)break;
            }
            if(flag==1)printf("No\n");
            else printf("Yes\n");
        }
    }
    return 0;
}